package com.appspot.entourage.ui.server;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import com.appspot.entourage.explorer.Explorer;
import com.appspot.entourage.ui.client.EntourageService;
import com.appspot.entourage.ui.shared.FieldVerifier;
import com.appspot.entourage.ui.shared.PersonResult;
import com.appspot.entourage.ui.shared.ProcessResult;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class EntourageServiceImpl extends RemoteServiceServlet implements
		EntourageService {
	boolean processStarted = false;
	
	Explorer explorer;
	
	int i = 0;
	
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}

	@Override
	public PersonResult[] getRelatedPeople(String personName) {
		List<PersonResult> toBeReturned = null;
		
		toBeReturned = new ArrayList<PersonResult>();
		
		PersonResult p1 = new PersonResult(1, "obama", null);
		PersonResult p2 = new PersonResult(2, "bush", null);
		PersonResult p3 = new PersonResult(3, "ataturk", null);
		PersonResult p4 = new PersonResult(4, "mustafa", p3);
		
		toBeReturned.add(p1);
		toBeReturned.add(p2);
		toBeReturned.add(p3);
		toBeReturned.add(p4);
		
		PersonResult ia[] = new PersonResult[toBeReturned.size()];
		ia = toBeReturned.toArray(ia);
		return ia;
		
	}

	@Override
	public ProcessResult startProcess(int maxRadius, String name) throws IllegalArgumentException {
		ProcessResult rslt = new ProcessResult();
		rslt.setResult(true);
		rslt.setResultStr("Process started successfully");
		
		if(!processStarted){
			
		}
		
		return rslt;
		
	}

	@Override
	public void continueTo() throws Exception {
		explorer.continueTo();
		
	}

	@Override
	public void stopProcess() throws Exception {
		explorer.stop();
		
	}

	@Override
	public void pause() throws Exception {
		explorer.pause();
		
	}

	@Override
	public ProcessResult getLogs(int MaxNumber) {
		ProcessResult rslt = new ProcessResult();
		if(i<10){
			
			rslt.setResult(false);
			rslt.setResultStr("It's being processed"+i);
			i++;
			
		}
		else{
			rslt.setResult(true);
			rslt.setResultStr("OK");
			i=0;
			
		}
		
		return rslt;
	}

	@Override
	public String getRelatedPeopleAsXml(String url) {
		return "ercu test mest";
	}
}
